<template >
    <!-- :style用来加背景图片 -->
    <div class = "login_container" :style="bg">
        <!-- 登录组件 -->
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
                <div class="text1" >
                    个人学习空间
                </div>
            </div>

            <!-- 表单 -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginformRules" label-width="80px" class="login_form">

                <el-form-item label="用户名" prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-key" type="password"></el-input>
                </el-form-item>

                <el-form-item  class="btns">
                    <el-button type="primary" plain @click="login">登录</el-button>
                    <el-button type="primary" plain @click="register">注册</el-button>
                    <el-button type="info" plain @click="resetLoginForm">重置 </el-button>
                </el-form-item> 

            </el-form>   
        
        </div>
    </div>

</template>

<script>
    // import Mock from './api/login.js';
    export default {
        data(){
            return {
                // 登录表单数据绑定对象
                loginForm:{
                    username:'admin',
                    password:'123456'
                },

                // 表单验证规则对象
                loginformRules:{
                    username:[
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password:[
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],   
                },

                bg:{
                    backgroundImage: 'url('+require('../assets/sky.jpeg')+')',
                    backgroundRepeat: 'no-repeat',
                    backgroundSize:'100% 100%',
                    }
            }
        },

        methods:{
            resetLoginForm(){
                this.$refs.loginFormRef.resetFields();
            },
            
            login(){
                //   登录之前的预验证
                this.$refs.loginFormRef.validate(async valid =>{
                    // console.log(valid);
                    // 根据验证的值来确定是否发起网络请求
                    if(!valid) return;
                    // 请求服务器要保证api服务器处于运行中
                    // 返回数据只要data是真实的
                    //请求地址 与  请求参数
                    const { data:res} = await this.$http.post('login', this.loginForm);
                    console.log(res);
                    // 状态码是200时成功，message弹框提示
                    if (res.status !=200) 
                        // return this.$message.error('登录失败！');
                        return this.$message.error(res.message); 
                    this.$message.success('登录成功！');
                    //将登录成功后的token，保存到客户端的session Storage中
                    window.sessionStorage.setItem("token",res.data.token)
                    window.sessionStorage.setItem("role",res.data.role)
                    //通过编程式导航跳转到后台主页，路由地址是/home
                    console.log(res.data.role)
                    if(res.data.role === "user")
                    this.$router.push("/Desktop").catch((e)=>{console.log(e)});
                    else
                      this.$router.push("/UserList")

                });
            },

            register(){
                this.$router.push("/Register");
            }
            
        }
    };
</script>

<style Lang= "less" scoped>
.login_container {
    background-color: rgb(30, 30, 54);
    height:100%;

}  

.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    opacity:0.9;
}

.text1{
    margin-top: 10px;
}

.avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
}
img{
    width: 100%;
    height:100%;
    border-radius: 50%;
    background-color: #eee ;
}

.btns{
    display: flex;
    justify-content:flex-end;
}

.login_form{
    position :absolute;
    bottom: 20px;
    width:100%;
    padding: 0 20px;
    box-sizing: border-box;

}




    
</style>